package com.exonline.backstage.controller.student;

import cn.hutool.core.util.IdUtil;
import com.exonline.commons.base.IBaseServlet;
import com.exonline.backstage.entity.Student;
import com.exonline.commons.utils.JsonUtils;
import com.exonline.commons.utils.RequestBodyConverter;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/student")
public class StudentServlet extends HttpServlet implements IBaseServlet {
ObjectMapper mapper = new ObjectMapper();
    StudentService service = new StudentService();
    int pageno = 1;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageno = Integer.parseInt(req.getParameter("pageno"));

        String method = req.getParameter("method");
        try {
            if ("query".equals(method)) {
                this.query(req, resp);
            } else if ("queryone".equals(method)) {
                this.queryOne(req, resp);
            } else if ("add".equals(method)) {
                this.add(req, resp);
            } else if ("edit".equals(method)) {
                this.edit(req, resp);
            } else if ("delete".equals(method)) {
                this.delete(req, resp);
            }
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }


    @Override
    public void query(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        resp.setHeader("Content-Type", "application/json;charset=utf-8");
        List<Student> students = service.queryAll(10, pageno);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", students);
        String jsonStr = JsonUtils.toJson(map);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonStr);
    }

    @Override
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        service.deleteById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        map.put("data", id);

        String jsonStr = JsonUtils.toJson(map);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonStr);
    }

    @Override
    public void queryOne(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        Student student = service.queryOne(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", student);
        String jsonStr = JsonUtils.toJson(map);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonStr);
    }


    @Override
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        service.update(name, phone, password, id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", id);
        String jsonStr = JsonUtils.toJson(map);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonStr);
    }

    @Override
    public void add(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = IdUtil.simpleUUID();
//        String name = req.getParameter("name");
//        String phone = req.getParameter("phone");
//        String password = req.getParameter("password");
//        service.add(id, name, phone, password);
        Student student = RequestBodyConverter.convertRequestBody(req, Student.class);
        student.setId(id);

        service.add(student);

//        System.out.println(student);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "添加成功");
        map.put("data", id);
        String jsonStr = JsonUtils.toJson(map);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonStr);
    }
}
